"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateMember = exports.createMember = void 0;
const MemberEntity_1 = require("../entity/MemberEntity");
async function createMember(data = {}) {
    const { userId, username, state, userType, classId, classname } = data;
    let dbUser = null;
    try {
        dbUser = await MemberEntity_1.MemberEntity.findOne({ where: { userId } });
        if (!dbUser) {
            dbUser = new MemberEntity_1.MemberEntity();
            dbUser.userId = userId;
            dbUser.username = username;
            dbUser.state = state;
            dbUser.userType = userType;
            dbUser.classId = classId;
            dbUser.classname = classname;
            dbUser.createTime = Date.now().toString();
            dbUser.lastActiveTime = Date.now().toString();
            dbUser = await dbUser.save();
        }
    }
    catch (error) {
        console.error(error);
        throw new Error(error);
    }
    return dbUser;
}
exports.createMember = createMember;
async function updateMember(data = {}) {
    const { userId } = data;
    let dbUser = null;
    try {
        dbUser = await MemberEntity_1.MemberEntity.findOne({ where: { userId } });
        if (dbUser) {
            dbUser = Object.assign(dbUser, data);
            dbUser.lastActiveTime = Date.now().toString();
            dbUser = await dbUser.save();
        }
        else {
            console.error('user are not fined!');
            throw new Error('user are not fined!');
        }
    }
    catch (error) {
        console.error(error);
        throw new Error(error);
    }
    return dbUser;
}
exports.updateMember = updateMember;
